//
//  BuyView.m
//  YiShangPin
//
//  Created by 衣尚品1 on 15/3/17.
//  Copyright (c) 2015年 yishangpin2. All rights reserved.
//

#import "BuyView.h"
#import "AppDelegate.h"

@implementation BuyView{
    NSArray *sizeArray;
    NSArray *colorArray;
    NSInteger index1;
    NSInteger index2;
    float height;
    UILabel *label1;
    UILabel *label2;
    BOOL isAlloc;
    AppDelegate *delegate;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (void)awakeFromNib{
    delegate = [[UIApplication sharedApplication]delegate];
    
    //tableview里显示的数据
    sizeArray = [[NSArray alloc] initWithObjects:@"---",@"S",@"M",@"L",@"XL",@"XL", nil];
    colorArray = [[NSArray alloc] initWithObjects:@"---",@"Beige",@"Black",@"Brown",@"Light blue",@"Pink",@"Rose", nil];
    //隐藏两个下拉的tableview
    self.sizeTableView.hidden = YES;
    self.colorTableView.hidden = YES;
    
    //获取白色buyview的高度
    height = self.littleBuyView.frame.size.height;
    
    self.coverButton.hidden = NO;
    
    //去掉tableview分割线的空白
    if ([self.sizeTableView respondsToSelector:@selector(setSeparatorInset:)])
    {
        [self.sizeTableView setSeparatorInset:UIEdgeInsetsZero];
    }
    if ([self.sizeTableView respondsToSelector:@selector(setLayoutMargins:)])
    {
        [self.sizeTableView setLayoutMargins:UIEdgeInsetsZero];
    }
    if ([self.colorTableView respondsToSelector:@selector(setSeparatorInset:)])
    {
        [self.colorTableView setSeparatorInset:UIEdgeInsetsZero];
    }
    if ([self.colorTableView respondsToSelector:@selector(setLayoutMargins:)])
    {
        [self.colorTableView setLayoutMargins:UIEdgeInsetsZero];
    }
}

#pragma mark - 设置tableview
//去掉tableview分割线的空白
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath

{
    
    if ([cell respondsToSelector:@selector(setSeparatorInset:)])
        
    {
        
        [cell setSeparatorInset:UIEdgeInsetsZero];
        
    }
    
    if ([cell respondsToSelector:@selector(setLayoutMargins:)])
        
    {
        
        [cell setLayoutMargins:UIEdgeInsetsZero];
        
    }
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    int a = 0;
    //设置的是哪个tableview就返回对应array的个数
    if ([tableView isEqual:self.sizeTableView]) {
        a = (int)sizeArray.count;
    }
    if ([tableView isEqual:self.colorTableView]) {
        a = (int)colorArray.count;
    }
    return a;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 35;
}

// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UINib *nib = [UINib nibWithNibName:@"buyTableViewCell" bundle:nil];
    UITableViewCell *cell = [[UITableViewCell alloc] init];
    
    //不同的tableview显示不同的内容
    if ([tableView isEqual:self.sizeTableView]) {
        [self.sizeTableView registerNib:nib forCellReuseIdentifier:@"BuyCell"];
        cell = [tableView dequeueReusableCellWithIdentifier:@"BuyCell"];
        cell.textLabel.text = sizeArray[indexPath.row];
    }
    if ([tableView isEqual:self.colorTableView]) {
        [self.colorTableView registerNib:nib forCellReuseIdentifier:@"BuyCell"];
        cell = [tableView dequeueReusableCellWithIdentifier:@"BuyCell"];
        cell.textLabel.text = colorArray[indexPath.row];
    }
    
    //根据array的个数设置tableview的高度
    if (sizeArray.count < 6) {
        self.sizeTableView.frame = CGRectMake(self.sizeButton.frame.origin.x, self.sizeButton.frame.origin.y + self.sizeButton.frame.size.height, self.sizeButton.frame.size.width - 20, 35 * sizeArray.count);
    }
    if (colorArray.count < 6) {
        self.colorTableView.frame = CGRectMake(self.colorButton.frame.origin.x, self.colorButton.frame.origin.y + self.colorButton.frame.size.height, self.colorButton.frame.size.width - 20, 35 * colorArray.count);
    }
    
    return cell;

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    //判断是哪个tableview的点击事件
    if ([tableView isEqual:self.sizeTableView]) {
        //设置选中的字符串为sizeButton的title
        [self.sizeButton setTitle:sizeArray[indexPath.row] forState:0];
        //隐藏该tableview
        self.sizeTableView.hidden = YES;
        //将当前的index存入变量
        index1 = indexPath.row;
    }
    if ([tableView isEqual:self.colorTableView]) {
        [self.colorButton setTitle:colorArray[indexPath.row] forState:0];
         self.colorTableView.hidden = YES;
         index2 = indexPath.row;
    }
    
    //避免重复创建lable(isAlloc)
    //判断两个选项是否都选择了
    //两个都选了
    if (index1 != 0 && index2 != 0 && isAlloc == NO) {
        //设置白色buyview的高度增加以显示价格
        self.littleBuyView.frame = CGRectMake(self.littleBuyView.frame.origin.x, self.littleBuyView.frame.origin.y, self.littleBuyView.frame.size.width, height + 30);
        
        //初始化两个新旧价格的lable
        
        label1 = [[UILabel alloc] initWithFrame:CGRectMake(self.littleBuyView.frame.size.width/4, self.littleBuyView.frame.size.height - 30, self.littleBuyView.frame.size.width/4, 25)];
        [label1 setTextColor:[UIColor grayColor]];
        //设置横线花掉旧的价格
        NSString *price = @"¥297";
        NSUInteger length = [price length];
        NSMutableAttributedString *attri = [[NSMutableAttributedString alloc] initWithString:price];
        [attri addAttribute:NSStrikethroughStyleAttributeName value:@(NSUnderlinePatternSolid | NSUnderlineStyleSingle) range:NSMakeRange(0, length)];
        [label1 setAttributedText:attri];
        
        label2 = [[UILabel alloc] initWithFrame:CGRectMake(self.littleBuyView.frame.size.width/4 * 2, self.littleBuyView.frame.size.height - 30, self.littleBuyView.frame.size.width/4, 25)];
        label2.text = @"¥97";
        
        //添加lable到littleBuyView
        [self.littleBuyView addSubview:label1];
        [self.littleBuyView addSubview:label2];
        
        //隐藏覆盖在buybutton上的button
//        self.coverButton.hidden = YES;
        self.coverButton.hidden = YES;
        
        isAlloc = YES;
    }
    
    
    //有一个或两个都没选
    if (index1 == 0 || index2 == 0) {
        //把拉长的view收起来
        self.littleBuyView.frame = CGRectMake(self.littleBuyView.frame.origin.x, self.littleBuyView.frame.origin.y, self.littleBuyView.frame.size.width, height);
        //从littlebuyview移除两个lable
        [label1 removeFromSuperview];
        [label2 removeFromSuperview];
        
        isAlloc = NO;
        
        //显示覆盖button
//        self.coverButton.hidden = NO;
        self.coverButton.hidden = NO;
    }
    
    
    
}

- (IBAction)clickCancelButton:(id)sender {
    //给Latest发送通知移除整个Buy View
//    [[NSNotificationCenter defaultCenter] postNotificationName:@"removeBuyView" object:nil];
    [self removeFromSuperview];
}

- (IBAction)clickSizeButton:(id)sender {
    self.sizeTableView.hidden = NO;
    self.colorTableView.hidden = YES;
}

- (IBAction)clickColorButton:(id)sender {
    self.sizeTableView.hidden = YES;
    self.colorTableView.hidden = NO;
}

- (IBAction)clickBuyButton:(id)sender {
    
    [delegate.ShoppingArray addObject:[NSString stringWithFormat:@"%ld", delegate.tag]];
    delegate.sideViewController.rootViewController.selectedIndex = 2;

    [self clickCancelButton:nil];
    
}

- (IBAction)clickBackGround:(id)sender {
    self.sizeTableView.hidden = YES;
    self.colorTableView.hidden = YES;
}
@end
